<?php
	class Shepherd_SupportController extends Sss_Controller_Shepherd
	{
		/**
		* The default action - show the home page
		*/
		private $_menuName = 'Support';

		public function init()
		{
			parent::init();
			$this->checkInstance();
			$handle           = $this->getRequest()->getParam('handle', Sss_Ultility_CommonViewParamater::UNDERFINE);
			$userInformation = new Zend_Session_Namespace(Sss_Ultility_SessionName::ACCOUNT_SESSION_SHEPHERD . $this->getCurrentHandle());
			if (!$userInformation->username) {
				$this->_redirect("/$handle/user/login");
			}
		}
		public function loginAction()
		{
			$handle = $this->getRequest()->getParam('handle', Sss_Ultility_CommonViewParamater::UNDERFINE);
			$this->_redirect("/$handle/user/login");
		}
		public function indexAction()
		{
			$handle = $this->getRequest()->getParam('handle', Sss_Ultility_CommonViewParamater::UNDERFINE);
			$this->_redirect("/$handle/support/otrs");
		}
		public function otrsAction()
		{
			try{
				$handle = $this->getRequest()->getParam('handle', Sss_Ultility_CommonViewParamater::UNDERFINE);
				//check permission
				if(Sss_Ultility_Common::in_array_field(Sss_Ultility_CommonViewParamater::PER_SUPPORT_OTRS, Sss_Ultility_CommonViewParamater::ALL, 'feature', 'action' ,$this->listRole(), true) != 1 ) {
					$this->_redirect("/$handle/support/manual");
				}
				//get breadscrumb
				$breadscrumbs = array(
				new Sss_CommonEntity_BreadScrumb($this->_menuName, ''),
				new Sss_CommonEntity_BreadScrumb(Resources::_('OTRS'), '#')
				);
				$this->view->assign(Sss_Ultility_CommonViewParamater::BREADSCRUMBS, $breadscrumbs);

				$sessionAutoLoginUrl = new Zend_Session_Namespace(Sss_Ultility_SessionName::SHEPHERD_AUTO_LOGIN_SESSION . $this->getCurrentHandle());
				$autoLogin = $sessionAutoLoginUrl->autourl;

				$this->view->assign("autolink",$autoLogin);
				// echo $autoLogin;
				if($autoLogin == null || $autoLogin == ''){
					$this->view->assign(Sss_Ultility_CommonViewParamater::MSG, Resources::_("LOGIN_OTRS_FAIL"));
				}
			}
			catch(Exception $e){
				Sss_Ultility_Common::logger()->log($e, Zend_Log::ERR);
				$this->redirect($e->getMessage(),$this->view->url(array(
				Sss_Ultility_CommonViewParamater::CONTROLLER => 'error',
				Sss_Ultility_CommonViewParamater::ACTION => 'error'
				)) );
			}
		}
		public function manualAction()
		{

			$handle = $this->getRequest()->getParam('handle', Sss_Ultility_CommonViewParamater::UNDERFINE);
			$get  = $this->getRequest()->getParams();
			$isSearch = isset($get['s']) && $get['s'] == 1;
			//check permission
			if(Sss_Ultility_Common::in_array_field(Sss_Ultility_CommonViewParamater::PER_SUPPORT_MANUALS, Sss_Ultility_CommonViewParamater::ALL, 'feature', 'action' ,$this->listRole(), true) != 1 ) {
				$this->_redirect("/$handle/support/sla");
			}
		
			$documentTable = new Application_Model_Mapper_Document();
			$instanceTable = new Application_Model_Mapper_Instance();
		
			//get order, page, limit param
			$filterParam[Sss_Ultility_CommonViewParamater::ORDERFIELD] = isset($get[Sss_Ultility_CommonViewParamater::ORDERFIELD]) ? $get[Sss_Ultility_CommonViewParamater::ORDERFIELD] : null;
			$filterParam[Sss_Ultility_CommonViewParamater::ORDERTYPE]  = isset($get[Sss_Ultility_CommonViewParamater::ORDERTYPE]) ? $get[Sss_Ultility_CommonViewParamater::ORDERTYPE] : null;
			$filterParam[Sss_Ultility_CommonViewParamater::PAGE] = isset($get[Sss_Ultility_CommonViewParamater::PAGE]) ? $get[Sss_Ultility_CommonViewParamater::PAGE] : 1;
		
			//param filter: omit title text in field
			$paramDocumentName    = $isSearch ? strcmp($this->getRequest()->getParam('documentname', Sss_Ultility_CommonViewParamater::UNDERFINE), Resources::_('MANUAL_NAME')) == 0 ? '' : $this->getRequest()->getParam('documentname', Sss_Ultility_CommonViewParamater::UNDERFINE) : '';
			$paramVendorName    = $isSearch ? strcmp($this->getRequest()->getParam('vendorname', Sss_Ultility_CommonViewParamater::UNDERFINE), Resources::_('VENDOR')) == 0 ? '' : $this->getRequest()->getParam('vendorname', Sss_Ultility_CommonViewParamater::UNDERFINE) : '';
			$paramDocumentType = $isSearch ? strcmp($this->getRequest()->getParam('documenttype', Sss_Ultility_CommonViewParamater::UNDERFINE), Resources::_('TYPE')) == 0 ? '' : $this->getRequest()->getParam('documenttype', Sss_Ultility_CommonViewParamater::UNDERFINE) : '';


			//get filter
			$filterParam['documentname']    = $this->getValueRequest(Sss_Ultility_CommonViewParamater::FIX . 'documentname', trim($paramDocumentName), Sss_Ultility_SessionName::OVIS_MANUAL_SESSION);
			$filterParam['vendorname']    = $this->getValueRequest(Sss_Ultility_CommonViewParamater::FIX . 'vendorname', trim($paramVendorName), Sss_Ultility_SessionName::OVIS_MANUAL_SESSION);
			$filterParam['documenttype'] = $this->getValueRequest(Sss_Ultility_CommonViewParamater::FIX . 'documenttype', trim($paramDocumentType), Sss_Ultility_SessionName::OVIS_MANUAL_SESSION);
		
			$instance = $instanceTable->getInstanceByHandle($handle);
			$filterParam['stocklocation'] = $instance->stocklocation;
		
			$filePath = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::FILEPATH, -1);
			if($filePath != -1) {
				Sss_Ultility_Common::downloadFile($filePath);
				$this->redirect($this->view->url(array(
						Sss_Ultility_CommonViewParamater::CONTROLLER => $this->view->controllerName,
						Sss_Ultility_CommonViewParamater::ACTION => $this->view->actionName
				)));
			}
		
			//get list
			$list = $documentTable->loadListManual($this->view->limit, 0, $filterParam);
			$row  = $list->list;
			$pagination = $list->pagination;
		
			$this->view->assign(Sss_Ultility_CommonViewParamater::FILTER_PARAM, $filterParam);
			$this->view->assign(Sss_Ultility_CommonViewParamater::ROW, $row);
			$this->view->assign(Sss_Ultility_CommonViewParamater::PAGINATION, $pagination);
		
			$arrQueryString = Sss_Ultility_AdministrationFunction::getQueyString($isSearch, $filterParam);
			$this->view->assign(Sss_Ultility_CommonViewParamater::QUERY_STRING, $arrQueryString['querystring']);
			$this->view->assign(Sss_Ultility_CommonViewParamater::ORDER_QUERY_STRING, $arrQueryString['oquerystring']);
			//action button
			$actionbuttons = array(
					new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::EXPORT, 'manual-export?export=1', Resources::_('EXPORT')),
					new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::PRINTF, 'manual-export?print=1', Resources::_('PRINT')),
					new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::SEARCH, 'javascript:hideShow(this);', Resources::_('SEARCH'))
			);
			$this->view->assign(Sss_Ultility_CommonViewParamater::ACTION_BUTTONS, $actionbuttons);
		
			//get breadscrumb
			$breadscrumbs = array(
					new Sss_CommonEntity_BreadScrumb($this->_menuName, ''),
					new Sss_CommonEntity_BreadScrumb(Resources::_('MANUAL'), '#')
			);
			$this->view->assign(Sss_Ultility_CommonViewParamater::BREADSCRUMBS, $breadscrumbs);
		}
		public function manualExportAction()
		{
			$excel = new PHPExcel();
			$excel->setActiveSheetIndex(0);



			$handle = $this->getRequest()->getParam('handle', Sss_Ultility_CommonViewParamater::UNDERFINE);

			//check permission
			if(Sss_Ultility_Common::in_array_field(Sss_Ultility_CommonViewParamater::PER_SUPPORT_MANUALS, Sss_Ultility_CommonViewParamater::ALL, 'feature', 'action' ,$this->listRole(), true) != 1 ) {
				$this->_redirect("/$handle/user/login");
			}

			$documentTable = new Application_Model_Mapper_Document();
			$instanceTable = new Application_Model_Mapper_Instance();

			//get order, page, limit param
			$filterParam[Sss_Ultility_CommonViewParamater::ORDERFIELD] = isset($get[Sss_Ultility_CommonViewParamater::ORDERFIELD]) ? $get[Sss_Ultility_CommonViewParamater::ORDERFIELD] : null;
			$filterParam[Sss_Ultility_CommonViewParamater::ORDERTYPE]  = isset($get[Sss_Ultility_CommonViewParamater::ORDERTYPE]) ? $get[Sss_Ultility_CommonViewParamater::ORDERTYPE] : null;                
			$filterParam[Sss_Ultility_CommonViewParamater::PAGE] = isset($get[Sss_Ultility_CommonViewParamater::PAGE]) ? $get[Sss_Ultility_CommonViewParamater::PAGE] : 1;
			//param filter: omit title text in field
			$paramDocumentName    = strcmp($this->getRequest()->getParam('documentname', Sss_Ultility_CommonViewParamater::UNDERFINE), Resources::_('MANUAL_NAME')) == 0 ? '' : $this->getRequest()->getParam('documentname', Sss_Ultility_CommonViewParamater::UNDERFINE);
			$paramVendorName    = strcmp($this->getRequest()->getParam('vendorname', Sss_Ultility_CommonViewParamater::UNDERFINE), Resources::_('VENDOR')) == 0 ? '' : $this->getRequest()->getParam('vendorname', Sss_Ultility_CommonViewParamater::UNDERFINE);
			$paramDocumentType = strcmp($this->getRequest()->getParam('documenttype', Sss_Ultility_CommonViewParamater::UNDERFINE), Resources::_('TYPE')) == 0 ? '' : $this->getRequest()->getParam('documenttype', Sss_Ultility_CommonViewParamater::UNDERFINE);


			//get filter
			$filterParam['documentname']    = $this->getValueRequest(Sss_Ultility_CommonViewParamater::FIX . 'documentname', trim($paramDocumentName), Sss_Ultility_SessionName::OVIS_MANUAL_SESSION);
			$filterParam['vendorname']    = $this->getValueRequest(Sss_Ultility_CommonViewParamater::FIX . 'vendorname', trim($paramVendorName), Sss_Ultility_SessionName::OVIS_MANUAL_SESSION);
			$filterParam['documenttype'] = $this->getValueRequest(Sss_Ultility_CommonViewParamater::FIX . 'documenttype', trim($paramDocumentType), Sss_Ultility_SessionName::OVIS_MANUAL_SESSION);

			$instance = $instanceTable->getInstanceByHandle($handle);
			$filterParam['stocklocation'] = $instance->stocklocation;

			$list       = $documentTable->loadListManual(-1, 0, $filterParam);
			$rowAll        = $list->list;
			$pagination = $list->pagination;

			$this->view->assign(Sss_Ultility_CommonViewParamater::ROW, $rowAll);  
			$this->view->assign(Sss_Ultility_CommonViewParamater::PAGINATION, $pagination);
			$count = ((($pagination->pagecurrent * Sss_Ultility_Setting::PAGE_SIZE) - Sss_Ultility_Setting::PAGE_SIZE));
			$columnNames = array(
			Resources::_('ID'),
			Resources::_('MANUAL_NAME'),
			Resources::_('VENDOR'),
			Resources::_('TYPE')
			);

			$arrData = array();
			$index = 0;
			foreach((array)$rowAll as $row) {
				$count++;
				$arrData[$index][] = $count;
				$arrData[$index][] = $row['documentname'];
				$arrData[$index][] = $row['vendorname'];
				$arrData[$index][] = $row['description'];
				$index++;
			}  
			if(isset($_GET['export'])) {
				$this->exportDataToExcel($columnNames, $arrData, Resources::_('MANUAL'));
			}
		}
		public function slaAction()
		{

			$handle = $this->getRequest()->getParam('handle', Sss_Ultility_CommonViewParamater::UNDERFINE);
			$get  = $this->getRequest()->getParams();
			$isSearch = isset($get['s']) && $get['s'] == 1;
			//check permission
			if(Sss_Ultility_Common::in_array_field(Sss_Ultility_CommonViewParamater::PER_SUPPORT_SLA, Sss_Ultility_CommonViewParamater::ALL, 'feature', 'action' ,$this->listRole(), true) == 1 ) {
				$documentTable = new Application_Model_Mapper_Document();
				$instanceTable = new Application_Model_Mapper_Instance();

				//get order, page, limit param
				$filterParam[Sss_Ultility_CommonViewParamater::ORDERFIELD] = isset($get[Sss_Ultility_CommonViewParamater::ORDERFIELD]) ? $get[Sss_Ultility_CommonViewParamater::ORDERFIELD] : null;
				$filterParam[Sss_Ultility_CommonViewParamater::ORDERTYPE]  = isset($get[Sss_Ultility_CommonViewParamater::ORDERTYPE]) ? $get[Sss_Ultility_CommonViewParamater::ORDERTYPE] : null;
				$filterParam[Sss_Ultility_CommonViewParamater::PAGE] = isset($get[Sss_Ultility_CommonViewParamater::PAGE]) ? $get[Sss_Ultility_CommonViewParamater::PAGE] : 1;

				//param filter: omit title text in field
				$paramDocumentName    = $isSearch ? strcmp($this->getRequest()->getParam('documentname', Sss_Ultility_CommonViewParamater::UNDERFINE), Resources::_('SLA_NAME')) == 0 ? '' : $this->getRequest()->getParam('documentname', Sss_Ultility_CommonViewParamater::UNDERFINE) : '';
				$paramStockLocationName    = $isSearch ? strcmp($this->getRequest()->getParam('stocklocationname', Sss_Ultility_CommonViewParamater::UNDERFINE), Resources::_('STOCK_LOCATION')) == 0 ? '' : $this->getRequest()->getParam('stocklocationname', Sss_Ultility_CommonViewParamater::UNDERFINE) : '';


				//get filter
				$filterParam['documentname']    = $this->getValueRequest(Sss_Ultility_CommonViewParamater::FIX . 'documentname', trim($paramDocumentName), Sss_Ultility_SessionName::OVIS_SLA_SESSION);
				$filterParam['stocklocationname']    = $this->getValueRequest(Sss_Ultility_CommonViewParamater::FIX . 'stocklocationname', trim($paramStockLocationName), Sss_Ultility_SessionName::OVIS_SLA_SESSION);
				$instance = $instanceTable->getInstanceByHandle($handle);
				$filterParam['stocklocation'] = $instance->stocklocation;

				$filePath = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::FILEPATH, -1);

				if($filePath != -1) {
					Sss_Ultility_Common::downloadFile($filePath);
					$this->redirect($this->view->url(array(
					Sss_Ultility_CommonViewParamater::CONTROLLER => $this->view->controllerName,
					Sss_Ultility_CommonViewParamater::ACTION => $this->view->actionName
					)));
				}

				//get list
				$list = $documentTable->loadListSLA($this->view->limit, 0, $filterParam,null,$instance->instanceid);
				$row  = $list->list;
				$pagination = $list->pagination;

				$this->view->assign(Sss_Ultility_CommonViewParamater::FILTER_PARAM, $filterParam);
				$this->view->assign(Sss_Ultility_CommonViewParamater::ROW, $row);
				$this->view->assign(Sss_Ultility_CommonViewParamater::PAGINATION, $pagination);

				$arrQueryString = Sss_Ultility_AdministrationFunction::getQueyString($isSearch, $filterParam);
				$this->view->assign(Sss_Ultility_CommonViewParamater::QUERY_STRING, $arrQueryString['querystring']);
				$this->view->assign(Sss_Ultility_CommonViewParamater::ORDER_QUERY_STRING, $arrQueryString['oquerystring']);
				//action button
				$actionbuttons = array(
				new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::EXPORT, 'SLA-export?export=1', Resources::_('EXPORT')),
				new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::PRINTF, 'SLA-export?print=1', Resources::_('PRINT')),
				new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::SEARCH, 'javascript:hideShow(this);', Resources::_('SEARCH'))
				);
				$this->view->assign(Sss_Ultility_CommonViewParamater::ACTION_BUTTONS, $actionbuttons);

				//get breadscrumb
				$breadscrumbs = array(
				new Sss_CommonEntity_BreadScrumb($this->_menuName, ''),
				new Sss_CommonEntity_BreadScrumb(Resources::_('SLA'), '#')
				);
				$this->view->assign(Sss_Ultility_CommonViewParamater::BREADSCRUMBS, $breadscrumbs);
			} else {
				$this->view->assign(Sss_Ultility_CommonViewParamater::ERRORMSG, Resources::_('SHEPHERH_ACCESS_TO_FEATURE'));  
			}
		}
		public function slaExportAction()
		{

			$handle = $this->getRequest()->getParam('handle', Sss_Ultility_CommonViewParamater::UNDERFINE);

			//check permission
			if(Sss_Ultility_Common::in_array_field(Sss_Ultility_CommonViewParamater::PER_SUPPORT_SLA, Sss_Ultility_CommonViewParamater::ALL, 'feature', 'action' ,$this->listRole(), true) != 1 ) {
				$this->_redirect("/$handle/user/login");
			}
			$documentTable = new Application_Model_Mapper_Document();
			$instanceTable = new Application_Model_Mapper_Instance();

			//get order, page, limit param
			$filterParam[Sss_Ultility_CommonViewParamater::ORDERFIELD] = isset($get[Sss_Ultility_CommonViewParamater::ORDERFIELD]) ? $get[Sss_Ultility_CommonViewParamater::ORDERFIELD] : null;
			$filterParam[Sss_Ultility_CommonViewParamater::ORDERTYPE]  = isset($get[Sss_Ultility_CommonViewParamater::ORDERTYPE]) ? $get[Sss_Ultility_CommonViewParamater::ORDERTYPE] : null;
			$filterParam[Sss_Ultility_CommonViewParamater::PAGE] = isset($get[Sss_Ultility_CommonViewParamater::PAGE]) ? $get[Sss_Ultility_CommonViewParamater::PAGE] : 1;
			//param filter: omit title text in field
			$paramDocumentName    = strcmp($this->getRequest()->getParam('documentname', Sss_Ultility_CommonViewParamater::UNDERFINE), Resources::_('SLA_NAME')) == 0 ? '' : $this->getRequest()->getParam('documentname', Sss_Ultility_CommonViewParamater::UNDERFINE);
			$paramStockLocationName    = strcmp($this->getRequest()->getParam('stocklocationname', Sss_Ultility_CommonViewParamater::UNDERFINE), Resources::_('STOCK_LOCATION')) == 0 ? '' : $this->getRequest()->getParam('stocklocationname', Sss_Ultility_CommonViewParamater::UNDERFINE);
			$instance = $instanceTable->getInstanceByHandle($handle);
			$filterParam['stocklocation'] = $instance->stocklocation;


			//get filter
			$filterParam['documentname']    = $this->getValueRequest(Sss_Ultility_CommonViewParamater::FIX . 'documentname', trim($paramDocumentName), Sss_Ultility_SessionName::OVIS_SLA_SESSION);
			$filterParam['stocklocationname']    = $this->getValueRequest(Sss_Ultility_CommonViewParamater::FIX . 'stocklocationname', trim($paramStockLocationName), Sss_Ultility_SessionName::OVIS_SLA_SESSION);

			$list = $documentTable->loadListSLA(-1, 0, $filterParam,null,$instance->instanceid);
			$rowAll  = $list->list;
			$pagination = $list->pagination;
			$this->view->assign(Sss_Ultility_CommonViewParamater::ROW, $rowAll);  
			$this->view->assign(Sss_Ultility_CommonViewParamater::PAGINATION, $pagination);
			$count = ((($pagination->pagecurrent * Sss_Ultility_Setting::PAGE_SIZE) - Sss_Ultility_Setting::PAGE_SIZE));
			$columnNames = array(
			Resources::_('ID'),
			Resources::_('SLA_NAME'),
			Resources::_('STOCK_LOCATION')
			);

			$arrData = array();
			$index = 0;
			foreach((array)$rowAll as $row) {
				$count++;
				$arrData[$index][] = $count;
				$arrData[$index][] = $row['documentname'];
				$arrData[$index][] = $row['stocklocationname'];
				$index++;				
			}
			if(isset($_GET['export'])) {
				$this->exportDataToExcel($columnNames, $arrData, Resources::_('SLA'));
			}
		}
	}
